<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\logic\CateLogic;
use app\admin\logic\GoodsLogic;
use app\admin\model\Category;
use think\facade\Request;
use think\facade\View;

class Goods extends BaseController
{
    public function cateDel()
    {
        $id=Request::get('id','');
        $del=Category::destroy($id);
        if ($del){
            $this->success('删除成功',url('Goods/cate'));
        }else{
            $this->error('网络波动,删除失败');
        }
    }

    public function cateRedel()
    {
        $id=Request::get('id','');
        if ($id!=''){
            $restore=Category::onlyTrashed()->find($id);
            $restore->restore();
            if ($restore){
                $this->success('还原成功',url('Goods/cate'));
            }else{
                $this->error('网络波动,还原失败');
            }
        }
        $list=Category::onlyTrashed()->select();
        View::assign('list',$list);
        return view();
    }

    public function cate()
    {
        $plist=Category::field(true)->select();
        View::assign('plist',$plist);
        $pid=CateLogic::getPid();
        $cate_list=Category::getCateList();
        $cate=GoodsLogic::getNextCate($cate_list,$pid);
        View::assign('cate_list',$cate);

        if (Request::isPost()){
            $post=Request::post();
            $post['addtime']=time();
            $img=Goods::upload('pic','cate_img','md5');
            if (!empty($img)){
                $post['pic']=$img;
            }
            $add=Category::create($post);
            if ($add){
                $this->success('添加成功',url('Goods/cate'));
            }else{
                $this->error('网络波动,添加失败,请重试');
            }
        }
        return view();
    }

    public function cateedit()
    {
        $pid=Request::get('pid','0');
        $list=Category::cateEditList();
        $parentVlue=Category::field(true)->find($pid);
        View::assign('pValue',$parentVlue);
        View::assign('list',['pid'=>$pid,'list'=>$list]);

        if (Request::isPost()){
            $post=Request::post();
            $data=[
                'id'=>$pid,
                'parentid'=>$post['parentid'],
                'name'=>$post['name'],
                'sort'=>$post['sort'],
            ];
            $image=Goods::upload('pic','cate_img','md5');
            if (!empty($image)){
                $data['pic']=$image;
            }
            $update=Category::update($data);
            if ($update){
                $this->success('修改成功',url('cate'));
            }else{
                $this->error('修改失败');
            }
        }
        return view();
    }

    public function goodAdd()
    {
        //接值ID用于查询和判断进行修改操作
        $id=Request::get('id');
        $cate_list=Category::getCateList();
        $parentid=0;
        $cate=GoodsLogic::getNextCate($cate_list,$parentid);
        View::assign('cate_list',$cate);

        if (Request::isPost()){
            $post = Request::post();
            $data = [
                'name' =>$post['name'],
                'pro_number'=>$post['pro_number'],
                'price' =>$post['price'],
                'price_shop'=>$post['price_shop'],
                'content'=>$post['content'],
                'sort'=>$post['sort'],
                'renqi'=>$post['renqi'],
                'num'=>$post['num'],
                'cid'=>$post['cid'],
                'company'=>$post['company']?:'',
                'is_show'=>isset($post['is_show'])?$post['is_show']:0,
                'is_down'=>isset($post['is_down'])?$post['is_down']:0,
                'is_hot'=>isset($post['is_hot'])?$post['is_hot']:0
            ];

            if ($id){
                //修改
                $data['updatetime']=time();
                $data['id']=$id;

                $image=Goods::upload('image','goods_img','md5');
                if (!empty($image)){
                    $data['photo_x']=$image;
                    $data['photo_d']=$image;
                }
                $img=Goods::upload('img','goods_imgs','md5');
                if (!empty($img)){
                    $data['photo_string']=$img;
                }
                //拼装添加数组

                try{
                    $res =\app\admin\model\Goods::update($data);
                    $msg="成功";
                }catch (\Exception $exception){
                    $res=false;
                    $msg=$exception->getMessage();
                }
            }else{
                $data['addtime']=time();
                $image=Goods::upload('image','goods_img','md5');
                $img=Goods::upload('img','goods_imgs','md5');
                //拼装添加的数组
                $data['photo_string']=$img;
                $data['photo_x']=$image;
                $data['photo_d']=$image;
                //执行添加
                try{
                    $res=\app\admin\model\Goods::goodsAdd($data);
                    $msg='成功';
                }catch (\Exception $exception){
                    $res=false;
                    $msg=$exception->getMessage();
                }
            }
            if ($res){
                $this->success('成功',url('goodList'));
            }else{
                $this->error($msg);
            }
        }else{
            //渲染添加页面
            if ($id){
                $info=\app\admin\model\Goods::getGoodsInfo($id);
                View::assign($info);
            }
            //查询分类
        }

        return view();
    }

    public function goodList()
    {
        $pid='0';
        $cate_list=Category::field(true)->select()->toArray();
        $cate=GoodsLogic::getNextCate($cate_list,$pid);
        View::assign('cate_list',$cate);
        //处理where条件
        $where=GoodsLogic::getWhere();
        //查询goods数据列表
        $list=\app\admin\model\Goods::getGoodsList($where);
        View::assign('list',$list);
        return view();
    }

    public function goodsDel()
    {
        $id=Request::get('id','');
        $del=\app\admin\model\Goods::destroy($id);
        if ($del){
            $this->success('删除成功',url('Goods/goodList'));
        }else{
            $this->error('网络波动,删除失败');
        }
    }

    public function goodRedel()
    {
        $id=Request::get('id','');
        if ($id!=''){
            $restore=\app\admin\model\Goods::onlyTrashed()->find($id);
            $restore->restore();
            if ($restore){
                $this->success('还原成功',url('Goods/goodList'));
            }else{
                $this->error('网络波动,还原失败');
            }

        }
        $list=\app\admin\model\Goods::onlyTrashed()->select();
        View::assign('list',$list);
        return view();
    }
}